﻿using System;
using System.IO;
using System.Collections.Generic;
using Flagwind.Services;
using Flagwind.Resources;

namespace Flagwind.Plugins.Commands
{
	internal static class Utility
	{
		public static void PrintPluginNode(ICommandOutlet output, PluginTreeNode node, ObtainMode obtainMode, int maxDepth)
		{
			if(node == null)
			{
				return;
			}

			output.Write(CommandOutletColor.DarkYellow, "[{0}]", node.NodeType);
			output.WriteLine(node.FullPath);
			output.Write(CommandOutletColor.DarkYellow, "Plugin File: ");

			if(node.Plugin == null)
			{
				output.WriteLine(CommandOutletColor.Red, "N/A");
			}
			else
			{
				output.WriteLine(node.Plugin.FilePath);
			}

			output.Write(CommandOutletColor.DarkYellow, "Node Properties: ");
			output.WriteLine(node.Properties.Count);

			if(node.Properties.Count > 0)
			{
				output.WriteLine(CommandOutletColor.Gray, "{");

				foreach(PluginExtendedProperty property in node.Properties)
				{
					output.Write(CommandOutletColor.DarkYellow, "\t" + property.Name);
					output.Write(" = ");
					output.Write(property.RawValue);

					if(property.Value != null)
					{
						output.Write(CommandOutletColor.DarkGray, " [");
						output.Write(CommandOutletColor.Blue, property.Value.GetType().FullName);
						output.Write(CommandOutletColor.DarkGray, "]");
					}

					output.WriteLine();
				}

				output.WriteLine(CommandOutletColor.Gray, "}");
			}

			output.WriteLine(CommandOutletColor.DarkYellow, "Children: {0}", node.Children.Count);
			if(node.Children.Count > 0)
			{
				output.WriteLine();

				foreach(var child in node.Children)
				{
					output.WriteLine(child);
				}
			}

			object value = node.UnwrapValue(obtainMode, null);
			if(value != null)
			{
				output.WriteLine();
				output.WriteLine(Flagwind.Runtime.Serialization.Serializer.Text.Serialize(value));
			}
		}
	}
}